﻿


using Admin.NET.Core;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Microsoft.AspNetCore.Mvc;
using Admin.EcsWeb.Service.PatCell.Dto;
using Mapster;
using Furion.FriendlyException;
using GeneralCommon.Models.SignalR;
using Admin.EcsWeb.Option;
using Furion.RemoteRequest.Extensions;
using GeneralCommon.Models.Server;
using Furion;
using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Authorization;
using GeneralCommon.Extensions;
using OBS.Internal;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Numeric;
using Admin.EcsWeb.Service.PatProduct.Dto;
using Microsoft.AspNetCore.Http;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using Magicodes.ExporterAndImporter.Excel;
using System.IO;
using Nest;

namespace Admin.EcsWeb.Service;
/// <summary>
/// PatCellService服务
/// </summary>
[ApiDescriptionSettings("ECS应用", Order = 100)]
public class PatCellService : IDynamicApiController, ITransient
{
    private readonly SqlSugarRepository<Entity.PatCell> _rep;
    private readonly SqlSugarRepository<Entity.PatProduct> _product;
    

    Dictionary<string, string> chanelKV = new Dictionary<string, string>();
    public PatCellService(SqlSugarRepository<Entity.PatCell> rep, SqlSugarRepository<Entity.PatProduct> product)
    {
        _rep = rep;

        _product = product;

        chanelKV.Add("1", "1");
        chanelKV.Add("2", "21");
        chanelKV.Add("3", "41");
        chanelKV.Add("4", "61");
        chanelKV.Add("5", "81");
        chanelKV.Add("6", "101");
        chanelKV.Add("7", "121");
        chanelKV.Add("8", "141");
        chanelKV.Add("9", "161");
        chanelKV.Add("10", "181");
        chanelKV.Add("11", "201");
        chanelKV.Add("12", "221");
        chanelKV.Add("13", "241");
        chanelKV.Add("14", "261");
        chanelKV.Add("15", "281");
        chanelKV.Add("16", "301");
        chanelKV.Add("17", "321");
        chanelKV.Add("18", "341");
        chanelKV.Add("19", "361");
        chanelKV.Add("20", "381");
        chanelKV.Add("21", "2");
        chanelKV.Add("22", "22");
        chanelKV.Add("23", "42");
        chanelKV.Add("24", "62");
        chanelKV.Add("25", "82");
        chanelKV.Add("26", "102");
        chanelKV.Add("27", "122");
        chanelKV.Add("28", "142");
        chanelKV.Add("29", "162");
        chanelKV.Add("30", "182");
        chanelKV.Add("31", "202");
        chanelKV.Add("32", "222");
        chanelKV.Add("33", "242");
        chanelKV.Add("34", "262");
        chanelKV.Add("35", "282");
        chanelKV.Add("36", "302");
        chanelKV.Add("37", "322");
        chanelKV.Add("38", "342");
        chanelKV.Add("39", "362");
        chanelKV.Add("40", "382");
        chanelKV.Add("41", "3");
        chanelKV.Add("42", "23");
        chanelKV.Add("43", "43");
        chanelKV.Add("44", "63");
        chanelKV.Add("45", "83");
        chanelKV.Add("46", "103");
        chanelKV.Add("47", "123");
        chanelKV.Add("48", "143");
        chanelKV.Add("49", "163");
        chanelKV.Add("50", "183");
        chanelKV.Add("51", "203");
        chanelKV.Add("52", "223");
        chanelKV.Add("53", "243");
        chanelKV.Add("54", "263");
        chanelKV.Add("55", "283");
        chanelKV.Add("56", "303");
        chanelKV.Add("57", "323");
        chanelKV.Add("58", "343");
        chanelKV.Add("59", "363");
        chanelKV.Add("60", "383");
        chanelKV.Add("61", "4");
        chanelKV.Add("62", "24");
        chanelKV.Add("63", "44");
        chanelKV.Add("64", "64");
        chanelKV.Add("65", "84");
        chanelKV.Add("66", "104");
        chanelKV.Add("67", "124");
        chanelKV.Add("68", "144");
        chanelKV.Add("69", "164");
        chanelKV.Add("70", "184");
        chanelKV.Add("71", "204");
        chanelKV.Add("72", "224");
        chanelKV.Add("73", "244");
        chanelKV.Add("74", "264");
        chanelKV.Add("75", "284");
        chanelKV.Add("76", "304");
        chanelKV.Add("77", "324");
        chanelKV.Add("78", "344");
        chanelKV.Add("79", "364");
        chanelKV.Add("80", "384");
        chanelKV.Add("81", "5");
        chanelKV.Add("82", "25");
        chanelKV.Add("83", "45");
        chanelKV.Add("84", "65");
        chanelKV.Add("85", "85");
        chanelKV.Add("86", "105");
        chanelKV.Add("87", "125");
        chanelKV.Add("88", "145");
        chanelKV.Add("89", "165");
        chanelKV.Add("90", "185");
        chanelKV.Add("91", "205");
        chanelKV.Add("92", "225");
        chanelKV.Add("93", "245");
        chanelKV.Add("94", "265");
        chanelKV.Add("95", "285");
        chanelKV.Add("96", "305");
        chanelKV.Add("97", "325");
        chanelKV.Add("98", "345");
        chanelKV.Add("99", "365");
        chanelKV.Add("100", "385");
        chanelKV.Add("101", "6");
        chanelKV.Add("102", "26");
        chanelKV.Add("103", "46");
        chanelKV.Add("104", "66");
        chanelKV.Add("105", "86");
        chanelKV.Add("106", "106");
        chanelKV.Add("107", "126");
        chanelKV.Add("108", "146");
        chanelKV.Add("109", "166");
        chanelKV.Add("110", "186");
        chanelKV.Add("111", "206");
        chanelKV.Add("112", "226");
        chanelKV.Add("113", "246");
        chanelKV.Add("114", "266");
        chanelKV.Add("115", "286");
        chanelKV.Add("116", "306");
        chanelKV.Add("117", "326");
        chanelKV.Add("118", "346");
        chanelKV.Add("119", "366");
        chanelKV.Add("120", "386");
        chanelKV.Add("121", "7");
        chanelKV.Add("122", "27");
        chanelKV.Add("123", "47");
        chanelKV.Add("124", "67");
        chanelKV.Add("125", "87");
        chanelKV.Add("126", "107");
        chanelKV.Add("127", "127");
        chanelKV.Add("128", "147");
        chanelKV.Add("129", "167");
        chanelKV.Add("130", "187");
        chanelKV.Add("131", "207");
        chanelKV.Add("132", "227");
        chanelKV.Add("133", "247");
        chanelKV.Add("134", "267");
        chanelKV.Add("135", "287");
        chanelKV.Add("136", "307");
        chanelKV.Add("137", "327");
        chanelKV.Add("138", "347");
        chanelKV.Add("139", "367");
        chanelKV.Add("140", "387");
        chanelKV.Add("141", "8");
        chanelKV.Add("142", "28");
        chanelKV.Add("143", "48");
        chanelKV.Add("144", "68");
        chanelKV.Add("145", "88");
        chanelKV.Add("146", "108");
        chanelKV.Add("147", "128");
        chanelKV.Add("148", "148");
        chanelKV.Add("149", "168");
        chanelKV.Add("150", "188");
        chanelKV.Add("151", "208");
        chanelKV.Add("152", "228");
        chanelKV.Add("153", "248");
        chanelKV.Add("154", "268");
        chanelKV.Add("155", "288");
        chanelKV.Add("156", "308");
        chanelKV.Add("157", "328");
        chanelKV.Add("158", "348");
        chanelKV.Add("159", "368");
        chanelKV.Add("160", "388");
        chanelKV.Add("161", "9");
        chanelKV.Add("162", "29");
        chanelKV.Add("163", "49");
        chanelKV.Add("164", "69");
        chanelKV.Add("165", "89");
        chanelKV.Add("166", "109");
        chanelKV.Add("167", "129");
        chanelKV.Add("168", "149");
        chanelKV.Add("169", "169");
        chanelKV.Add("170", "189");
        chanelKV.Add("171", "209");
        chanelKV.Add("172", "229");
        chanelKV.Add("173", "249");
        chanelKV.Add("174", "269");
        chanelKV.Add("175", "289");
        chanelKV.Add("176", "309");
        chanelKV.Add("177", "329");
        chanelKV.Add("178", "349");
        chanelKV.Add("179", "369");
        chanelKV.Add("180", "389");
        chanelKV.Add("181", "10");
        chanelKV.Add("182", "30");
        chanelKV.Add("183", "50");
        chanelKV.Add("184", "70");
        chanelKV.Add("185", "90");
        chanelKV.Add("186", "110");
        chanelKV.Add("187", "130");
        chanelKV.Add("188", "150");
        chanelKV.Add("189", "170");
        chanelKV.Add("190", "190");
        chanelKV.Add("191", "210");
        chanelKV.Add("192", "230");
        chanelKV.Add("193", "250");
        chanelKV.Add("194", "270");
        chanelKV.Add("195", "290");
        chanelKV.Add("196", "310");
        chanelKV.Add("197", "330");
        chanelKV.Add("198", "350");
        chanelKV.Add("199", "370");
        chanelKV.Add("200", "390");
        chanelKV.Add("201", "11");
        chanelKV.Add("202", "31");
        chanelKV.Add("203", "51");
        chanelKV.Add("204", "71");
        chanelKV.Add("205", "91");
        chanelKV.Add("206", "111");
        chanelKV.Add("207", "131");
        chanelKV.Add("208", "151");
        chanelKV.Add("209", "171");
        chanelKV.Add("210", "191");
        chanelKV.Add("211", "211");
        chanelKV.Add("212", "231");
        chanelKV.Add("213", "251");
        chanelKV.Add("214", "271");
        chanelKV.Add("215", "291");
        chanelKV.Add("216", "311");
        chanelKV.Add("217", "331");
        chanelKV.Add("218", "351");
        chanelKV.Add("219", "371");
        chanelKV.Add("220", "391");
        chanelKV.Add("221", "12");
        chanelKV.Add("222", "32");
        chanelKV.Add("223", "52");
        chanelKV.Add("224", "72");
        chanelKV.Add("225", "92");
        chanelKV.Add("226", "112");
        chanelKV.Add("227", "132");
        chanelKV.Add("228", "152");
        chanelKV.Add("229", "172");
        chanelKV.Add("230", "192");
        chanelKV.Add("231", "212");
        chanelKV.Add("232", "232");
        chanelKV.Add("233", "252");
        chanelKV.Add("234", "272");
        chanelKV.Add("235", "292");
        chanelKV.Add("236", "312");
        chanelKV.Add("237", "332");
        chanelKV.Add("238", "352");
        chanelKV.Add("239", "372");
        chanelKV.Add("240", "392");
        chanelKV.Add("241", "13");
        chanelKV.Add("242", "33");
        chanelKV.Add("243", "53");
        chanelKV.Add("244", "73");
        chanelKV.Add("245", "93");
        chanelKV.Add("246", "113");
        chanelKV.Add("247", "133");
        chanelKV.Add("248", "153");
        chanelKV.Add("249", "173");
        chanelKV.Add("250", "193");
        chanelKV.Add("251", "213");
        chanelKV.Add("252", "233");
        chanelKV.Add("253", "253");
        chanelKV.Add("254", "273");
        chanelKV.Add("255", "293");
        chanelKV.Add("256", "313");
        chanelKV.Add("257", "333");
        chanelKV.Add("258", "353");
        chanelKV.Add("259", "373");
        chanelKV.Add("260", "393");
        chanelKV.Add("261", "14");
        chanelKV.Add("262", "34");
        chanelKV.Add("263", "54");
        chanelKV.Add("264", "74");
        chanelKV.Add("265", "94");
        chanelKV.Add("266", "114");
        chanelKV.Add("267", "134");
        chanelKV.Add("268", "154");
        chanelKV.Add("269", "174");
        chanelKV.Add("270", "194");
        chanelKV.Add("271", "214");
        chanelKV.Add("272", "234");
        chanelKV.Add("273", "254");
        chanelKV.Add("274", "274");
        chanelKV.Add("275", "294");
        chanelKV.Add("276", "314");
        chanelKV.Add("277", "334");
        chanelKV.Add("278", "354");
        chanelKV.Add("279", "374");
        chanelKV.Add("280", "394");
        chanelKV.Add("281", "15");
        chanelKV.Add("282", "35");
        chanelKV.Add("283", "55");
        chanelKV.Add("284", "75");
        chanelKV.Add("285", "95");
        chanelKV.Add("286", "115");
        chanelKV.Add("287", "135");
        chanelKV.Add("288", "155");
        chanelKV.Add("289", "175");
        chanelKV.Add("290", "195");
        chanelKV.Add("291", "215");
        chanelKV.Add("292", "235");
        chanelKV.Add("293", "255");
        chanelKV.Add("294", "275");
        chanelKV.Add("295", "295");
        chanelKV.Add("296", "315");
        chanelKV.Add("297", "335");
        chanelKV.Add("298", "355");
        chanelKV.Add("299", "375");
        chanelKV.Add("300", "395");
        chanelKV.Add("301", "16");
        chanelKV.Add("302", "36");
        chanelKV.Add("303", "56");
        chanelKV.Add("304", "76");
        chanelKV.Add("305", "96");
        chanelKV.Add("306", "116");
        chanelKV.Add("307", "136");
        chanelKV.Add("308", "156");
        chanelKV.Add("309", "176");
        chanelKV.Add("310", "196");
        chanelKV.Add("311", "216");
        chanelKV.Add("312", "236");
        chanelKV.Add("313", "256");
        chanelKV.Add("314", "276");
        chanelKV.Add("315", "296");
        chanelKV.Add("316", "316");
        chanelKV.Add("317", "336");
        chanelKV.Add("318", "356");
        chanelKV.Add("319", "376");
        chanelKV.Add("320", "396");
        chanelKV.Add("321", "17");
        chanelKV.Add("322", "37");
        chanelKV.Add("323", "57");
        chanelKV.Add("324", "77");
        chanelKV.Add("325", "97");
        chanelKV.Add("326", "117");
        chanelKV.Add("327", "137");
        chanelKV.Add("328", "157");
        chanelKV.Add("329", "177");
        chanelKV.Add("330", "197");
        chanelKV.Add("331", "217");
        chanelKV.Add("332", "237");
        chanelKV.Add("333", "257");
        chanelKV.Add("334", "277");
        chanelKV.Add("335", "297");
        chanelKV.Add("336", "317");
        chanelKV.Add("337", "337");
        chanelKV.Add("338", "357");
        chanelKV.Add("339", "377");
        chanelKV.Add("340", "397");
        chanelKV.Add("341", "18");
        chanelKV.Add("342", "38");
        chanelKV.Add("343", "58");
        chanelKV.Add("344", "78");
        chanelKV.Add("345", "98");
        chanelKV.Add("346", "118");
        chanelKV.Add("347", "138");
        chanelKV.Add("348", "158");
        chanelKV.Add("349", "178");
        chanelKV.Add("350", "198");
        chanelKV.Add("351", "218");
        chanelKV.Add("352", "238");
        chanelKV.Add("353", "258");
        chanelKV.Add("354", "278");
        chanelKV.Add("355", "298");
        chanelKV.Add("356", "318");
        chanelKV.Add("357", "338");
        chanelKV.Add("358", "358");
        chanelKV.Add("359", "378");
        chanelKV.Add("360", "398");
        chanelKV.Add("361", "19");
        chanelKV.Add("362", "39");
        chanelKV.Add("363", "59");
        chanelKV.Add("364", "79");
        chanelKV.Add("365", "99");
        chanelKV.Add("366", "119");
        chanelKV.Add("367", "139");
        chanelKV.Add("368", "159");
        chanelKV.Add("369", "179");
        chanelKV.Add("370", "199");
        chanelKV.Add("371", "219");
        chanelKV.Add("372", "239");
        chanelKV.Add("373", "259");
        chanelKV.Add("374", "279");
        chanelKV.Add("375", "299");
        chanelKV.Add("376", "319");
        chanelKV.Add("377", "339");
        chanelKV.Add("378", "359");
        chanelKV.Add("379", "379");
        chanelKV.Add("380", "399");
        chanelKV.Add("381", "20");
        chanelKV.Add("382", "40");
        chanelKV.Add("383", "60");
        chanelKV.Add("384", "80");
        chanelKV.Add("385", "100");
        chanelKV.Add("386", "120");
        chanelKV.Add("387", "140");
        chanelKV.Add("388", "160");
        chanelKV.Add("389", "180");
        chanelKV.Add("390", "200");
        chanelKV.Add("391", "220");
        chanelKV.Add("392", "240");
        chanelKV.Add("393", "260");
        chanelKV.Add("394", "280");
        chanelKV.Add("395", "300");
        chanelKV.Add("396", "320");
        chanelKV.Add("397", "340");
        chanelKV.Add("398", "360");
        chanelKV.Add("399", "380");
        chanelKV.Add("400", "400");
    }

    /// <summary>
    /// 分页查询PatCellService
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "Page")]
    public async Task<SqlSugarPagedList<PatCellOutput>> Page([FromQuery] PatCellInput input)
    {
        var query = _rep.AsQueryable()
                    .WhereIF(!string.IsNullOrWhiteSpace(input.TRAYID), u => u.TRAYID.Contains(input.TRAYID.Trim()))
                    .WhereIF(!string.IsNullOrWhiteSpace(input.CELLID), u => u.CELLID.Contains(input.CELLID.Trim()))

                    .Select<PatCellOutput>()
;
        query = query.OrderBuilder(input);
        return await query.ToPagedListAsync(input.Page, input.PageSize);
    }

    /// <summary>
    /// 增加PatCellService
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Add")]
    public async Task Add(AddPatCellInput input)
    {
        var entity = input.Adapt<Entity.PatCell>();
        await _rep.InsertAsync(entity);
    }

    /// <summary>
    /// 新增patcell数据
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "AddAll")]
    [AllowAnonymous]
    public async Task AddAll(List<AddPatCellInput> inputs)
    {

        var entity = inputs.Adapt<List<Entity.PatCell>>();
        var datas = entity.Distinct(new PatCompare());
        try
        {
            await _rep.Context.Ado.BeginTranAsync();
            await _rep.DeleteAsync(x => true);
            await _rep.InsertRangeAsync(datas.ToArray());
            await _rep.Context.Ado.CommitTranAsync();
        }
        catch (Exception ex)
        {
            throw Oops.Oh(ex.Message);
        }
    }

    /// <summary>
    /// 传输patcell数据
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "pattofms")]
    public async Task PatToFms()
    {

        var pats = await _rep.GetListAsync();

        if (pats == null || pats.Count == 0)
        {
            throw Oops.Oh("没有推送的数据");
        }
        if (pats.FirstOrDefault(x => x.Message.IsNotNullOrEmpty()) != null)
        {
            if (pats.Count == 1)
                throw Oops.Oh("无法推送包含错误信息的数据");
        }
        // 进行数据转换

        try
        {
            var products = await _product.GetListAsync();
            var trayDic = pats.Where(x => x.TRAYID.IsNotNullOrEmpty()).ToLookup(x => x.TRAYID).ToDictionary(x => x.Key, y => y.ToList());
            if (trayDic.Keys.Count > 100)
            {
                throw Oops.Oh("推送数据不能超过100个托盘");
            }
            string result = string.Empty;
            foreach (var key in trayDic.Keys)
            {
                result = await CommitTrayCells(trayDic[key], products);
            }
            if (result.IsNotNullOrEmpty())
            {
                throw Oops.Oh(result);
            }
        }
        catch (Exception ex)
        {
            throw Oops.Oh(ex.Message);
        }
    }

    private async Task<string> CommitTrayCells(List<Entity.PatCell> trays, List<Entity.PatProduct> products)
    {
        var datas = trays.Distinct(new PatCompare());
        List<TrayCell> cells = new List<TrayCell>();
        foreach (var patdata in datas)
        {
            if (patdata.CHANNEL == 0)
            {
                throw new Exception($"{patdata.TRAYID},电芯通道号为0。");
            }
            cells.Add(new TrayCell
            {
                Channel = int.Parse(chanelKV[patdata.CHANNEL.ToString()]),
                BarCode = patdata.CELLID
            });
        }
        int lineNo = 0;
        var cell = datas.FirstOrDefault();
        if (cell.CELLID.IsNullOrEmpty())
        {
            throw Oops.Oh($"{cell.TRAYID} 电芯码为空");
        }
        var lineCode = cell.CELLID.Substring(0, 1);
        if (lineCode == "D")
            lineNo = 13;
        else if (lineCode == "E")
            lineNo = 14;
        else if (lineCode == "F")
            lineNo = 15;
        else if (lineCode == "G")
            lineNo = 16;
        else if (lineCode == "H")
            lineNo = 17;
        else if (lineCode == "J")
            lineNo = 18;

        var name = products.FirstOrDefault(x => x.NAME == cell.CELLID.Substring(6, 1));

        if (name == null)
        {
            throw Oops.Oh($"请先配置{cell.CELLID.Substring(6, 1)}的型号信息");
        }

        TransToFMS tran = new TransToFMS
        {
            LineCode = cell.CELLID.Substring(0, 1),
            LineNo = lineNo,
            TrayNo = cell.TRAYID,
            LotNo = cell.CELLID.Substring(1, 3),
            PreIr = cell.IR,
            PreV = cell.OCV,

            ProductModel = name.DES,
            ProductType = cell.TRAYID.Substring(0, 1),
            TrayCells = cells
        };
        var url = $"{App.GetOptions<GrpcOptions>().FmsUrl}/api/mes/tray/group";
        var result = await url.SetBody(tran).PostAsAsync<ReturnModel<string>>();



        return result.Msg;
    }

    /// <summary>
    /// 增加PatCellService
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "remotecell")]
    public async Task PushRemoteCell(InformClientModel inform)
    {

        try
        {
            var url = $"{App.GetOptions<GrpcOptions>().GrpcApiUrl}/api/message/informclient";
            var result = await url.SetBody(inform).PostAsAsync<ReturnModel<string>>();

            if (!result.IsSuccess)
            {
                throw Oops.Oh(result.Msg);
            }
        }
        catch (Exception ex)
        {
            throw Oops.Oh(ex.Message);
        }
    }

    /// <summary>
    /// 删除PatCellService
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Delete")]
    public async Task Delete(DeletePatCellInput input)
    {
        var entity = await _rep.GetFirstAsync(u => u.Id == input.ID) ?? throw Oops.Oh(ErrorCodeEnum.D1002);
        await _rep.DeleteAsync(entity);   //真删除
    }

    /// <summary>
    /// 更新PatCellService
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost]
    [ApiDescriptionSettings(Name = "Update")]
    public async Task Update(UpdatePatCellInput input)
    {
        var entity = input.Adapt<Entity.PatCell>();
        await _rep.AsUpdateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
    }

    /// <summary>
    /// 获取PatCellService
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "Detail")]
    public async Task<Entity.PatCell> Get([FromQuery] QueryByIdPatCellInput input)
    {
        return await _rep.GetFirstAsync(u => u.Id == input.ID);
    }

    /// <summary>
    /// 获取PatCellService列表
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "List")]
    public async Task<List<PatCellOutput>> List([FromQuery] PatCellInput input)
    {
        return await _rep.AsQueryable().Select<PatCellOutput>().ToListAsync();
    }

    /// <summary>
    /// 上传文件
    /// </summary>
    /// <param name="file"></param>
    /// <param name="path"></param>
    /// <returns></returns>
    [DisplayName("上传文件")]
    [ApiDescriptionSettings(Name = "upload")]
    [HttpPost]
    public async Task<bool> UploadFile(IEnumerable<IFormFile> files)
    {
        
        if (files == null || files.Count()==0)
        {
            return false;
        }
      

            List<AddPatCellInput> datas = new List<AddPatCellInput>();
            foreach(var file in files)
            {
                MemoryStream ms = new MemoryStream();
                await file.OpenReadStream().CopyToAsync(ms);


                var result = new ExcelImporter().Import<PatCellImport>(ms);

                if (result.Result.HasError)
                {
                    throw Oops.Oh($"{file.Name} 导入错误");
                }


                var pats = result.Result.Data.Adapt<List<AddPatCellInput>>();

                datas.AddRange(pats);
               
            }
            await AddAll(datas);



        
     

        return true;
        
    }
    /// <summary>
    /// 下载模板
    /// </summary>
    /// <returns></returns>
    [ApiDescriptionSettings(Name = "template")]
    [HttpGet]
    public async Task<string> DownloadTemplate()
    {
       
        var filePath = Path.Combine(App.WebHostEnvironment.WebRootPath, "组装电芯模板.xlsx");
        if (File.Exists(filePath))
        {
            return new FileInfo(filePath).Name;
        }

        var result = await new ExcelImporter().GenerateTemplate<PatCellImport>(filePath);

        return new FileInfo(filePath).Name;
    }
    /// <summary>
    /// 导出数据
    /// </summary>
    /// <returns></returns>
    [ApiDescriptionSettings(Name = "export")]
    [HttpGet]
    public async Task<string> ExportPatCell()
    {
       var datas= await List(new PatCellInput());
        if(datas.Count == 0)
        {
            throw Oops.Oh("没有数据，无法导出");
        }
        var filePath = Path.Combine(App.WebHostEnvironment.WebRootPath, "组装电芯导出.xlsx");

        if (File.Exists(filePath)) File.Delete(filePath);

        var exports = datas.Adapt<List<PatCellExport>>();

        var result= await new ExcelExporter().Export(filePath, exports);

        return new FileInfo(filePath).Name;
    }




}

